﻿(function ($) {
    $.fn.swBanner = function (options) {
        var defaults = {
            animateTime: 300,
            delayTime: 5000
        };
        var setting = $.extend({}, defaults, options);
        return this.each(function () {
            var $obj = $(this);
            //var o=setting.animateTime;
            var d = setting.delayTime;
            var $oban = $obj.find(".banList li");
            var _len = $oban.length;
            var $nav = $obj.find(".fomW a");
            var _index = 0;
            var timer;

            function showImg(n) {
                $oban.eq(n).addClass("active").siblings().removeClass("active");
                $nav.eq(n).addClass("current").siblings().removeClass("current");
            }

            function player() {
                showImg($obj.find(".fomW").find("a.current").index() % _len);
                timer = setInterval(function () {
                    var _index = $obj.find(".fomW").find("a.current").index();
                    showImg((_index + 1) % _len);
                }, d)
            }

            $nav.click(function () {
                if (!($oban.is(":animated"))) {
                    _index = $(this).index();
                    showImg(_index);
                }
            });
            var move;
            $(".banList").on('touchstart', function (e) {
                move = 0;
                clearInterval(timer);
                var touch = e.originalEvent,
                    startX = touch.changedTouches[0].pageX, startY = touch.changedTouches[0].pageY;
                $(".banList").on('touchmove', function (e) {
                    touch = e.originalEvent.touches[0] ||
                        e.originalEvent.changedTouches[0];
                    if (touch.pageX - startX < -20) {
                        move = 1;
                    }else if (touch.pageX - startX > 20) {
                        move = -1;
                    }else{
                        move = 0;
                    }
                });
            }).on('touchend', function () {
                showImg(($(".banList .active").index() + move)%$(".banList li").length);
                player();
            });
            player();
        });
    }
})(jQuery);